<template>
    <div class="goods-list">
        <!--        属性绑定-->
        <!--        两种跳转方式：a  window.location.href-->
<!--                <div class="goods-item" v-for="item in goodslist" :key="item.id">-->
        <div class="goods-item" v-for="item in goodslist"
             :key="item.id"
             @click="goDetail(item.id)"
        >

            <img :src="item.img_url" alt="">
            <h3 class="title">{{item.title}}</h3>
            <div class="info">
                <p class="price">
                    <span class="new">￥{{item.sell_price}}</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{item.stock_quantity}}件</span>
                </p>
            </div>


        </div>
        <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>


    </div>
</template>

<script>
    export default {
        data() {
            return {
                pageindex: 1,
                goodslist: []
            }
        },
        created() {
            this.getGoodsList();
        },
        methods: {
            getGoodsList() {
                // 获取商品列表
                this.$http.get('api/getgoods?pageindex=' + this.pageindex).then(result => {
                    console.log(result.body.message);
                    if (result.body.status === 0) {
                        // this.goodslist = result.body.message;
                        this.goodslist = this.goodslist.concat(result.body.message)

                    }
                })
            },
            getMore() {
                // 根据最新的页面值，加载数据，拼接起来，
                this.pageindex++;
                this.getGoodsList()

            },
            goDetail(id) {
                // 使用js实现路由导航
                console.log(this);
                // 区分 this.$route 和this.$router
                // this.$route:是路由参数对象，所有路由中的参数 params,query 都属于他
                // this.$router:是路由导航对象，用他可以方便执行js代码，实现路由的前进，后退，跳转到新的url地址
// 最简单的
                // this.$router.push("/home/goodsinfo/"+id)
          // 传递对象
                // this.$router.push({path:"/home/goodsinfo/"+id})
                this.$router.push({path:"/home/goodsinfo/"+id})

            }
        }

    }
</script>

<style lang="less">
    .goods-list {
        display: flex;
        flex-wrap: wrap;
        padding: 8px;
        justify-content: space-between;

        .goods-item {
            width: 49%;
            border: 1px solid #ccc;
            box-shadow: 0 0 8px #ccc;
            margin: 3px 0;
            padding: 2px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            min-height: 293px;

            img {
                width: 100%;
            }

            .title {
                font-size: 14px;
                margin-bottom: -10px;
            }

            .info {
                background-color: #eee;
                padding: 2px;
                margin-top: -14px;

                .price {
                    .old {
                        text-decoration: line-through;
                        font-size: 12px;
                        margin-left: 12px;
                    }

                    .new {
                        color: red;
                        font-weight: bold;
                        font-size: 16px;
                    }
                }

                .sell {
                    display: flex;
                    justify-content: space-between;
                    font-size: 12px;
                    margin-bottom: 0px;
                }
            }
        }
    }

</style>